//渲染页面
var cart_list = (function () {
    var storage = localStorage.getItem("carts");
    if (storage === null) {
        return [];
    }
    return JSON.parse(storage);
})();
// var total_price = 0;
function render() {
    var odiv = "";
    for(var i = 0; i < cart_list.length; i++) {
        var price = (cart_list[i].data.normal_price / 100).toFixed(2);
        var count = cart_list[i].count;
        // var now_price = price * count;
        // total_price += now_price;
        odiv += `<div class="cart_list" data-index="${i}">
                    <div class="cart_list_wrap clearfix">
                        <div class="check">
                            <input type="checkbox" class="ischeck">
                        </div>
                        <div class="cart_img clearfix">
                            <img src="${cart_list[i].data.hd_thumb_url}" alt="">
                            <h1>${cart_list[i].data.short_name}</h1>
                        </div>
                        <div class="single_price">
                            <p>${price}</p>
                        </div>
                        <div class="count">
                            <div class="num_act">
                                <button>-</button>
                                <input type="text" value="${count}">
                                <button>+</button>
                            </div>
                        </div>
                        <div class="total">
                            <p>${price * count}</p>
                        </div>
                        <div class="delete">
                            <button class="del_goods">删除</button>
                        </div>
                    </div>
                    <div class="cart_amount">
                        <div class="amount">
                            商品总价：
                            <span>￥<b>${(cart_list[i].data.normal_price / 100).toFixed(2) * cart_list[i].count}</b></span>
                        </div>
                    </div>
                </div>`
    }
    $(".render").html(odiv);
}
render();

function calculate(){
    //点击减号按钮  数量和小计会改变
    $("button:contains(-)").click(function(){
        var index = $(this).parents(".cart_list").attr("data-index");
        cart_list[index].count --;
        if(cart_list[index].count-- <=0){
            cart_list[index].count =0;
        }
        location.reload();
        localStorage.setItem("carts",JSON.stringify(cart_list));
    })
    //点击加号按钮，数量和小计会改变
    $("button:contains(+)").click(function(){
        var index = $(this).parents(".cart_list").attr("data-index");
        cart_list[index].count ++;
        location.reload();
        localStorage.setItem("carts",JSON.stringify(cart_list));
    })
    //总价
    // $(".t_price b").html(total_price.toFixed(2));
}
calculate();

//点击删除按钮把这条数据删除
function del(){
    $(".del_goods").click(function(){
        var index = $(this).parents(".cart_list").attr("data-index");
        console.log(index)
        for(var i = 0; i < cart_list.length; i++) {
            if(index == i){
                cart_list.splice(index,1);
                //console.log(cart_list)
                localStorage.setItem("carts", JSON.stringify(cart_list));
                location.reload();
            }
        }
    })
}
del();
//点击结算按钮，判断用户是否登录，没有登录就让其登录；
//若登录了，就跳转到确认订单页面；填写收货人信息
function buy(){
    $(".buy").click(function(){
        alert("此功能还未实现，敬请期待")
    })
}
buy();

//checkbox是否选中，功能实现
function check(){
    var total_p = 0;
    $(".render").on("click",".ischeck",function(){
        var flag = $(this).is(":checked");
        console.log(flag)
        if(flag == true){
            total_p += Number($(this).parents(".cart_list").find(".cart_amount").find(".amount").find("b").html());
            $(".t_price b").html(total_p);
        }else if(flag == false){
            total_p -= Number($(this).parents(".cart_list").find(".cart_amount").find(".amount").find("b").html());
            $(".t_price b").html(total_p);
        }
    })
}
check();